package com.koron.device.service.impl;

import com.alibaba.fastjson.JSON;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.util.CodeTools;
import com.koron.device.bean.ChangeEquip;
import com.koron.device.bean.EquipmentChangeParam;
import com.koron.device.bean.EquipmentChangeQuery;
import com.koron.device.bean.EquipmentChangeVO;
import com.koron.device.mapper.EquipmentChangeMapper;
import com.koron.device.service.EquipmentChangeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author qingfeng
 * @description: 设备变更service
 * @date 2021/7/14 15:00
 */
@Service
@Slf4j
public class EquipmentChangeServiceImpl implements EquipmentChangeService {

    @Autowired
    private WorkFlowHandler workFlowHandler;
    @Autowired
    private HttpServletRequest httpServletRequest;
    /**
     * 更新
     *
     * @param factory
     * @param vo
     * @return
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public EquipmentChangeVO saveOrUpdate(SessionFactory factory, EquipmentChangeVO vo) {
        EquipmentChangeMapper mapper = factory.getMapper(EquipmentChangeMapper.class);
        vo.setId(StringUtils.isBlank(vo.getId()) ? CodeTools.getCode32() : vo.getId());
        vo.setCode(StringUtils.isBlank(vo.getCode()) ? CodeTools.getCodeNo("SBYD", "yyyyMM", 3) : vo.getCode());
        mapper.saveOrUpdate(vo);
        List<ChangeEquip> equipments = vo.getEquipments();
        // 删除异动设备
        mapper.deleteEquipByChangeId(vo.getId());
        Assert.notEmpty(equipments, "异动设备不能为空");
        equipments.stream().forEach(row -> {
            row.setId(CodeTools.getCode32());
            row.setChangeId(vo.getId());
        });
        // 添加新的异动设备
        mapper.batchInsertEquips(equipments);
        List<EquipmentChangeParam> params = vo.getParams();
        if(CollectionUtils.isNotEmpty(params)) {
            params.stream().forEach(row -> {
                row.setId(CodeTools.getCode32());
                row.setChangeId(vo.getId());
            });
            // 删除异动参数
            mapper.deleteParamByChangeId(vo.getId());
            // 添加新的异动参数
            mapper.batchInsertParams(params);
        }
        return vo;
    }

    @Override
    @TaskAnnotation("list")
    public List<EquipmentChangeVO> list(SessionFactory factory, EquipmentChangeQuery query) {
        EquipmentChangeMapper mapper = factory.getMapper(EquipmentChangeMapper.class);
        List<EquipmentChangeVO> list = mapper.list(query,httpServletRequest.getHeader("Language"));
        return list;
    }

    @Override
    @TaskAnnotation("getById")
    public EquipmentChangeVO getById(SessionFactory factory, String id) {
        EquipmentChangeMapper mapper = factory.getMapper(EquipmentChangeMapper.class);
        EquipmentChangeVO equipmentChange = mapper.getDetailById(id);
        return equipmentChange;
    }

    @Override
    @TaskAnnotation("delete")
    public Boolean delete(SessionFactory factory, String id) {
        EquipmentChangeMapper mapper = factory.getMapper(EquipmentChangeMapper.class);
        Assert.hasText(id, "id不能为空");
        List<String> ids = Arrays.asList(id.split(","));
        mapper.delete(ids);
        // 删除设备异动的工作流
        ids.stream().forEach(row -> workFlowHandler.delete(id));
        return true;
    }

    /**
     * 工作流更新
     *
     * @param factory
     * @param redirectQuery
     */
    @Override
    @TaskAnnotation("workflowUpdate")
    public Boolean workflowUpdate(SessionFactory factory, RedirectQuery redirectQuery) {
        Integer operation = redirectQuery.getOperation();
        EquipmentChangeVO vo = JSON.parseObject(redirectQuery.getFormVariables(), EquipmentChangeVO.class);
        switch (operation) {
            case 256: // 撤回
            case 1: // 驳回
                // 驳回的节点编号
                // 驳回到草稿节点
                vo.setStatus("0");
                saveOrUpdate(factory, vo);
                break;
            case 32: // 同意
                vo.setStatus("1");
                saveOrUpdate(factory, vo);
        }
        return true;
    }

    @Override
    @TaskAnnotation("updateEnd")
    public Boolean updateEnd(SessionFactory factory, RedirectQuery redirectQuery) {
        EquipmentChangeMapper mapper = factory.getMapper(EquipmentChangeMapper.class);
        EquipmentChangeVO vo = JSON.parseObject(redirectQuery.getFormVariables(), EquipmentChangeVO.class);
        vo.setStatus("2");
        saveOrUpdate(factory, vo);
        mapper.batchUpdateParams(vo.getParams());
        return true;
    }
}
